package com.star.spring.my.service.service;

import com.star.spring.my.service.entity.AlertEntity;
import com.star.spring.my.service.mapper.AlertMapper;
import com.star.spring.my.service.model.SensorData;
import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.builder.KieModule;
import org.kie.api.io.ResourceType;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.internal.io.ResourceFactory;
import org.kie.internal.utils.KieHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import static com.star.spring.my.service.config.drool.DroolsConfig.RULES_CUSTOMER_RULES_DRL;

@Service
public class RuleEngineService {

    @Autowired
    private AlertMapper alertMapper;


    public void applyRules(SensorData sensorData) {
//        KieSession kieSession = kieContainer.newKieSession();
        KieSession kieSession = getKieSession2();

        try {
            kieSession.insert(sensorData);
            kieSession.fireAllRules();
            if (Boolean.TRUE.equals(sensorData.getAlarm())) {
                AlertEntity alertEntity = new AlertEntity();
                alertEntity.setDeviceId(sensorData.getDeviceId());
                alertEntity.setValue(sensorData.getValue());
                alertMapper.insert(alertEntity);
            }
        } finally {
            kieSession.dispose();
        }
    }

    public KieSession getKieSession() {
        KieServices kieServices = KieServices.Factory.get();
        KieFileSystem kieFileSystem = kieServices.newKieFileSystem();


        kieFileSystem.write(ResourceFactory.newClassPathResource(RULES_CUSTOMER_RULES_DRL));
        KieBuilder kb = kieServices.newKieBuilder(kieFileSystem);
        kb.buildAll();
        KieModule kieModule = kb.getKieModule();
        KieContainer kieBase = kieServices.newKieContainer(kieModule.getReleaseId());

        return kieBase.newKieSession();
    }

    public KieSession getKieSession2() {


        String data = " import com.star.spring.my.service.model.SensorData\n" + "\n" + "\n" + "rule \"rule1\"\n" + "when\n" + "    $sensorData : SensorData(value > 10)\n" + "then\n" + "    System.out.println(\"value >10\");\n" + "end\n" + "\n" + "rule \"rule2\"\n" + "when\n" + "    $sensorData : SensorData(value < 10)\n" + "then\n" + "    System.out.println(\"value <10\");\n" + "end\n" + "\n" + "rule \"rule3\"\n" + "when\n" + "    $sensorData : SensorData(value > 10 && value<20)\n" + "then\n" + "    System.out.println(\"value between 10 and 20\");\n" + "end\n" + "\n" + "// Add more rules as needed for different sensors and regions\n";
        KieHelper kieHelper = new KieHelper();
        kieHelper.addContent(data, ResourceType.DRL);
        return kieHelper.build().newKieSession();

    }


    public static void writeText2File(File file, String text) {
        FileWriter writer = null;
        try {
            writer = new FileWriter(file);
            char[] chars = text.toCharArray();
            writer.write(chars);
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static void writeToFile() {
        // 讲规则写入文本
        try {

            String data = " import com.star.spring.my.service.model.SensorData\n" + "\n" + "\n" + "rule \"rule1\"\n" + "when\n" + "    $sensorData : SensorData(value > 10)\n" + "then\n" + "    System.out.println(\"value >10\");\n" + "end\n" + "\n" + "rule \"rule2\"\n" + "when\n" + "    $sensorData : SensorData(value < 10)\n" + "then\n" + "    System.out.println(\"value <10\");\n" + "end\n" + "\n" + "rule \"rule3\"\n" + "when\n" + "    $sensorData : SensorData(value > 10 && value<20)\n" + "then\n" + "    System.out.println(\"value between 10 and 20\");\n" + "end\n" + "\n" + "// Add more rules as needed for different sensors and regions\n";
            String path = "/Users/star/javaProjects/springboot-project/my-service/src/main/resources/";
            File file = new File(path + RULES_CUSTOMER_RULES_DRL);
            //if file doesnt exists, then create it
            if (!file.exists()) {
                file.createNewFile();
            }
            writeText2File(file, data);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
